import redis

class RedisOpration(object):
    '''
    测试redis连通性
    清理redis缓存
    '''
    def __init__(self,host=None,port=None,db=None,password=None):
        self.host = host
        self.port = port
        self.db = db
        self.password = password

    def TestRedisConnect(self):
        '''
        测试连通性
        :return:
        '''
        try:
            redis.Redis(host=self.host,port=self.port,password=self.password,decode_responses=True,socket_timeout=3).ping()
        except TimeoutError as e:
            raise e
        else:
            return {'msg':'Redis连接成功'}

    def CleanRedisDB(self):
        '''
        清理缓存
        :return:
        '''
        try:
            connect = redis.Redis(host=self.host,port=self.port,password=self.password,db=self.db,decode_responses=True,socket_timeout=3)
        except TimeoutError as e:
            raise e
        else:
            if self.db is None:
                connect.flushall()
            else:
                connect.flushdb()
            return {'msg':'清理成功'}
